<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/26 0026
 * Time: 13:56
 */
namespace app\supplies\controller;
use app\index\controller\Base;
use \think\Db;
use \think\Config;
use \think\Session;
use \think\Cookie;
use think\config\driver\Json;
class Index extends Base{
    //主页
    public function index(){
        if(Cookie::get('user')){
            $this->assign('id', Cookie::get('user'));$this->assign('supplier_id', Cookie::get('user')['id']);
            return view(APP_PATH.request()->module().'/view/index.html');
        }else{
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
    }
    //首页
    public  function indexHome(){
        if(Cookie::get('user')){
            $user = Cookie::get('user');
            $this->assign('id', $user['id']);
            return view(APP_PATH.request()->module().'/view/html/indexHome.html');
        }else{
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
    }

    //供应商管理
    public  function supply(){
        if(Cookie::get('user')){
            $user = Cookie::get('user');
            return view(APP_PATH.request()->module().'/view/html/supply/supply.html',[
                'id' => $user['id']
            ]);
        }else{
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
    }

    //修改页面
    public  function editInfo(){
        if(Cookie::get('user')){
            $categoryData = Db::name('base_data')->where(['level'=>0,'id'=>['<',5]])->select();//类别
            $this->assign('categoryData',$categoryData);
            $user = Cookie::get('user');
            return view(APP_PATH.request()->module().'/view/html/shop/shop.html',[
                'id' => $user['id']
            ]);
        }else{
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }

    }
    //账号密码登录页
    public function login(){
        if(Cookie::get('user')){
            $user = Cookie::get('user');
            $this->assign('id', $user['id']);
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }else{
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }

    }
    //验证码登录页
    public function smsLogin(){
        if(!Cookie::get('user')){
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
        return view(APP_PATH.request()->module().'/view/html/smsLogin.html');
    }
    //注册页
    public function register(){
        if(!Cookie::get('user')){
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
        return view(APP_PATH.request()->module().'/view/html/register.html');
    }
    //完善商户信息
    public function authenticationInfo(){
        $this->assign('id',input('id'));
        return view(APP_PATH.request()->module().'/view/html/authenticationInfo.html');
    }
    //忘记密码页面
    public function resetPassword(){
        if(!Cookie::get('user')){
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
        return view(APP_PATH.request()->module().'/view/html/resetPassword.html');
    }
    //认证审核中页面
    public function certification(){
        if(!Cookie::get('user')){
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
        return view(APP_PATH.request()->module().'/view/html/authentication/certification.html');
    }
    //认证审核失败页面
        public function failCertification(){
            if(!Cookie::get('user')){
                return view(APP_PATH.request()->module().'/view/html/login.html');
            }
            return view(APP_PATH.request()->module().'/view/html/authentication/authenticationFailure.html');
        }
    //交易统计
        public function transaction(){
            if(!Cookie::get('user')){
                return view(APP_PATH.request()->module().'/view/html/login.html');
            }
            $user = cookie('user');
            $this->assign('id',$user['id']);
            return view(APP_PATH.request()->module().'/view/html/transaction/transaction.html');
        }
    //订单页面
    public function myOrder(){
        if(!Cookie::get('user')){
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
        return view(APP_PATH.request()->module().'/view/html/myOrder/myorder.html');
    }
    //订单列表
    public  function listOrder(){
        if(!Cookie::get('user')){
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
        return view(APP_PATH.request()->module().'/view/html/myOrder/list-order.html');
    }
     //订单详情
    public  function detailsOrder(){
        if(!Cookie::get('user')){
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
        $id          = input('id');
        $supplier_id = input('supplier_id');
        if(empty($id)){
            return view(APP_PATH.request()->module().'/view/html/myOrder/list-order.html');
        }
        $wha['a.id'] = $id;
        $Re = Db::name('order')
            ->alias('a')
            ->field('a.*,c.addr,c.postcode,c.name,c.tel')
            ->join("user c", "a.user_id = c.id", 'left')
            ->where($wha)
            ->find();
        $whe['b.order_id'] = $id;
        $Res = Db::name('order_info')
            ->alias('b')
            ->field('b.*,b.price*b.num as total,d.image,d.name,d.status')
            ->join("product d", "b.product_id = d.id", 'left')
            ->where($whe)
            ->select();
        if($Re&&$Res){
            $result = self::setStatusStr($Re);//获取订单状态值
            //分离日期和时间点
            $result['addTime']      = partDate($Re['add_time']);//下单时间
            $result['deliveryTime'] = partDate($Re['delivery_time']);//商家确认发货时间
            $result['receiptTime']  = partDate($Re['receipt_time']);//用户收货时间
            $result['commentTime']  = partDate($commentTime = Db::name('comment')->where(['order_id'=>$id])->value('time'));//用户评论时间
            $result['deleteTime']   = partDate($Re['receipt_time']);//用户删除时间
            //获取优惠的价格
            $disMoney = $Re['money']-$Re['paymoney'];
            $result['disMoney'] = $disMoney;
            $result['info'] = $Re;
            $address = $Re['addr'];
            $address = explode(',', $address);
            $str = '';
            if(count($address) > 1){
                if(isset($address[0])){
                    $str .= preg_replace('# #','',$address[0]).'&nbsp;';
                }
                if(isset($address[1])){
                    $str .= preg_replace('# #','',$address[1]).'&nbsp;';
                }
                if(isset($address[2])){
                    $str .= preg_replace('# #','',$address[2]).'&nbsp;';
                }
                if(isset($address[3])){
                    $str .= preg_replace('# #','',$address[3]);
                }
            }
            $result['addr']     = $str;
            $result['count']    = count($Res);
            $result['list']     = $Res;
            //获取优惠活动
            $result['supplier_discount'] = Db::table('discount')->order('id desc')->where(['supplier_id'=>$supplier_id])->value('type');
        }else{
            $result['list'] = '';
        }
//        dump($result);
        $this->assign('data',$result);;
        return view(APP_PATH.request()->module().'/view/html/myOrder/details-order.html');
    }
     //订单操作
      public  function operationOrder(){
          if(!Cookie::get('user')){
              return view(APP_PATH.request()->module().'/view/html/login.html');
          }
          $this->assign('categoryData',self::getBaseData()['categoryData']);
          $this->assign('kindData',self::getBaseData()['kindData']);
          $this->assign('smallTypeData',self::getBaseData()['smallTypeData']);
          $this->assign('brandData',self::getBaseData()['brandData']);
          return view(APP_PATH.request()->module().'/view/html/myOrder/operation-order.html');
       }
     //订单评价
      public  function evaluationOrder(){
          if(!Cookie::get('user')){
              return view(APP_PATH.request()->module().'/view/html/login.html');
          }
          $this->assign('categoryData',self::getBaseData()['categoryData']);
          $this->assign('kindData',self::getBaseData()['kindData']);
          $this->assign('smallTypeData',self::getBaseData()['smallTypeData']);
          $this->assign('brandData',self::getBaseData()['brandData']);
          return view(APP_PATH.request()->module().'/view/html/myOrder/evaluation-order.html');
      }
 //我的 页面
    public function notification(){
        if(!Cookie::get('user')){
            return view(APP_PATH.request()->module().'/view/html/login.html');
        }
        return view(APP_PATH.request()->module().'/view/html/notification/notification.html');
    }
    private  function getBaseData(){
        $categoryData = Db::name('base_data')->where(['level'=>0,'flag'=>1,'parent_id'=>0,'type_menu'=>1])->select();//类别
        $kindData = Db::name('base_data')->where(['level'=>1])->select();//种类
        $smallTypeData = Db::name('base_data')->where(['level'=>2])->select();//品类
        $brandData = Db::name('base_data')->where(['type_menu'=>2,'flag'=>1,'level'=>0])->select();//品牌
        $list['categoryData'] = $categoryData;
        $list['kindData'] = $kindData;
        $list['smallTypeData'] = $smallTypeData;
        $list['brandData'] = $brandData;
        return $list;
    }

    static private function setStatusStr($Re){
        $result = [];
        if(1==$Re['status']){
            $result['order_status'] = '待处理 <br/> <span class="color-6">pending</span>';
            $result['order_process'] = '你的订单正在处理';
            $result['order_process_english'] = 'your order is being processed';
        }
        if(2==$Re['status']){
            $result['order_status'] = '待发货 <br/> <span class="color-6">pending delivery</span>';
            $result['order_process'] = '商家已经确定你的订单，正在发货中';
            $result['order_process_english'] = 'your order has already been processed, and will be delivered to you';
            /*$result['order_process'] = '你的订单正在处理';
            $result['order_process_english'] = 'your order is being processed';*/
        }
        if(3==$Re['status']){
            /*$result['order_status'] = '待确认 <br/> <span class="color-6">to be confirmed</span>';
            $result['order_process'] = '你的货物已送到，待用户确认';
            $result['order_process_english'] = 'your goods have been sent to the user to confirm';*/
            $result['order_status'] = '交易成功<br/> <span class="color-6">transaction successful</span>';
            $result['order_process'] = '交易成功';
            $result['order_process_english'] = 'transaction successful';
        }
       /* if(4==$Re['status']){
            $result['order_status'] = '已确认 <br/> <span class="color-6">confirmed</span>';
            $result['order_process'] = '你的订单已经送到，待用户评论';
            $result['order_process_english'] = 'your order has been sent to the user to comment';
        }
        if(5==$Re['status']){
            $result['order_status'] = '待评价<br/> <span class="color-6">evaluated</span>';
            $result['order_process'] = '你的订单已经完成，正等待你的评价';
            $result['order_process_english'] = 'your order was successfully completed, we await your feedback';
        }*/
        if(4==$Re['status']){
            $result['order_status'] = '交易成功<br/> <span class="color-6">transaction successful</span>';
            $result['order_process'] = '交易成功';
            $result['order_process_english'] = 'transaction successful';
        }
        if(6==$Re['status']){
            $result['order_status'] = '交易关闭 <br/> <span class="color-6">transaction closed</span>';
            $result['order_process'] = '交易已关闭';
            $result['order_process_english'] = 'transaction closed';
        }
        return $result;
    }
}